import tkinter as tk
from tkinter import messagebox
import data as da

# 定义全局变量
size = 10
data = da.成长型  # 这里简化了数据，实际项目中应该从文件或数据库读取
models = {}
read = {}



# 左侧统计分词
def str_left(words):
    strs = []
    for i in range(len(words)):
        strs.append(''.join(words[:i+1]))
    return strs
# 右侧统计分词
def str_right(words):
    strs = []
    for i in range(len(words)):
        strs.append(''.join(words[len(words)-i-1:]))
    return strs
# 单字
def str_one(words):
    return words

# 简单统计
def count(data):
    global models
    for l in range(len(data)):
        key = data[l]
        keys = list(key)
        # 计算权重
        weight = sum(1/k for k in range(1, len(keys)+1))
        models[key] = models.get(key, 0) + weight

# 需要识别的关键词
def my_keys(data):
    global read
    for l in range(len(data)):
        key = data[l]
        read[key] = 0

# 识别文章
def send():
    global read, models
    # 初始化关键词
    read = {}
    val = input_entry.get()
    if val!= '':
        words = list(val)
        sum = 0  # 加权和
        my_keys(str_left(words))
        my_keys(str_right(words))
        my_keys(str_one(words))
        for key in read:
            # 命中了关键词
            if key in models:
                print('命中：',key,models.get(key, 0))
                sum += models.get(key, 0)
        # 判断阈值，这里设置阈值为30
        if sum > 30:
            messagebox.showinfo('结果', '成长型')
        else:
            messagebox.showinfo('结果', '非成长型')
        print(read, sum)

# 汉字与位置的组合：方便矩阵运算（略）
def array(data):
    str = ''  # 字符串集合
    max = 0  # 对话的长度
    for a in data:
        if len(a) > max:
            max = len(a)
        str = str + a
    words = list(set(str))
    word_arr = [[words[l] for l in range(len(words))] for i in range(max)]
    print(word_arr)

# 初始化数据
for d in  range(len(data)):
    words = list(data[d])
    # 开始统计
    count(str_left(words))
    count(str_right(words))
    count(str_one(words))
    print(models)
    # 其它组合

# 创建主窗口
root = tk.Tk()
root.title('财务报表分析')

# 创建输入框和按钮
input_entry = tk.Entry(root)
input_entry.pack()
input_entry.insert(0, '利润增长')
send_button = tk.Button(root, text='开始阅读', command=send)
send_button.pack()

# 运行主循环
root.mainloop()